﻿using System;

namespace Yz.Core.Tools
{
    public class YzUniqueCcode
    {
        /// <summary>
        /// 获得long型的Guid
        /// </summary>
        /// <returns></returns>
        public static long GetGuidLong()
        {
            byte[] bytes = Guid.NewGuid().ToByteArray();
            return BitConverter.ToInt64(bytes, 0);
        }

        /// <summary>
        /// 获得16位Guid
        /// </summary>
        /// <returns></returns>
        public static string GetGuid16()
        {
            long i = 1;
            byte[] bytes = Guid.NewGuid().ToByteArray();
            foreach (byte b in bytes)
            {
                i *= (b + 1);
            }
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }
        /// <summary>
        /// 获得Guid，去除短线
        /// </summary>
        /// <returns></returns>
        public static string GetGuid()
        {
            return Guid.NewGuid().ToString("N");
        }
        /// <summary>
        /// 获得Base64编码的Guid
        /// </summary>
        /// <returns></returns>
        public static string GetGuidBase64()
        {
            Guid guid = Guid.Empty;
            while (Guid.Empty == guid)
            {
                guid = Guid.NewGuid();
            }
            // Uses base64 encoding the guid.(Or  ASCII85 encoded)
            // But not recommend using Hex, as it is less efficient.
            return Convert.ToBase64String(guid.ToByteArray());
        }
    }
}
